package com.xugui.learn.pay.service;

import com.xugui.learn.pay.pojo.enums.PayChannel;
import com.xugui.learn.pay.pojo.request.NotifyRequest;
import com.xugui.learn.pay.pojo.request.PayRequest;
import com.xugui.learn.pay.pojo.request.RefundRequest;
import com.xugui.learn.pay.pojo.response.*;
import com.xugui.learn.pay.strategy.PayStrategy;
import com.xugui.learn.pay.strategy.PayStrategyFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class PayManager {

    private final PayStrategyFactory payStrategyFactory;

    /**
     * 统一支付接口
     *
     * @param request 支付请求参数
     * @return 支付响应参数
     */
    public PayResponse pay(PayRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(request.getPayChannel());
        return strategy.pay(request);
    }

    /**
     * 查询订单
     *
     * @param request 查询订单请求参数
     * @return 查询订单响应参数
     */
    public QueryResponse queryOrder(PayRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(request.getPayChannel());
        return strategy.queryOrder(request);
    }

    /**
     * 关闭订单
     *
     * @param request 关闭订单请求参数
     * @return 关闭订单响应参数
     */
    public CloseResponse close(PayRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(request.getPayChannel());
        return strategy.close(request);
    }

    /**
     * 退款
     *
     * @param payChannel 支付渠道
     * @param request    退款请求参数
     * @return 退款响应参数
     */
    public RefundResponse refund(PayChannel payChannel, RefundRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(payChannel);
        return strategy.refund(request);
    }

    /**
     * 查询退款
     *
     * @param payChannel 支付渠道
     * @param request    退款查询请求参数
     * @return 退款查询响应参数
     */
    public RefundResponse queryRefund(PayChannel payChannel, RefundRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(payChannel);
        return strategy.queryRefund(request);
    }

    /**
     * 处理回调
     *
     * @param request 回调请求参数
     * @return 回调响应参数
     */
    public NotifyResponse handleNotify(NotifyRequest request) {
        PayStrategy strategy = payStrategyFactory.getStrategy(request.getPayChannel());
        return strategy.handleNotify(request);
    }
}